package org.study.swing.study01.component;

import com.google.common.collect.Lists;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

/**
 * 作用描述
 * 参考 https://blog.csdn.net/hujyhfwfh2/article/details/89635542
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月14日
 */
public class ComboBoxDemo3 {
    public static void main(String[] args) {
        // testComboBox1();
        // testComboBox2();
        // testComboBox3();
        // testComboBox3_1();
        // testComboBox3_2();
        testComboBox4();
    }

    public static void testComboBox1() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：组合框");

        // 创建container容器
        Container container = frame.getContentPane();
        // 创建组合框数据容器：使用数组方式提供数据
        Object[] items = new String[]{"张三", "李四", "王五"};
        // 创建组合框
        JComboBox<Object> comboBox = new JComboBox<>(items);
        comboBox.addItem(123);
        // 设置位置
        comboBox.setBounds(10, 10, 100, 22);
        // 设置组合框可编辑，默认为false
        comboBox.setEditable(true);
        JButton button = new JButton("确定");
        button.setBounds(120, 10, 75, 22);
        // 添加监听器 打印选择
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 下面两个获取item的内容是一样的
                System.out.println(comboBox.getSelectedItem());
            }
        });
        container.add(comboBox);
        container.add(button);

        // 设置空布局管理器
        frame.setLayout(null);
        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testComboBox2() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：组合框");

        // 创建container容器
        Container container = frame.getContentPane();
        // 创建组合框数据容器：使用DefaultComboBoxModel方式提供数据，推荐
        DefaultComboBoxModel<Object> comboBoxModel = new DefaultComboBoxModel<>();
        comboBoxModel.addElement("张三");
        comboBoxModel.addElement("李四");
        comboBoxModel.addElement("王五");
        comboBoxModel.addElement(123);
        // 创建组合框
        JComboBox<Object> comboBox = new JComboBox<>(comboBoxModel);
        // 设置位置
        comboBox.setBounds(10, 10, 100, 22);
        // 设置组合框可编辑，默认为false
        comboBox.setEditable(true);
        JButton button = new JButton("确定");
        button.setBounds(120, 10, 75, 22);
        // 添加监听器 打印选择
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 下面两个获取item的内容是一样的
                System.out.println(comboBox.getSelectedItem());
                System.out.println(comboBoxModel.getSelectedItem());
            }
        });
        container.add(comboBox);
        container.add(button);

        // 设置空布局管理器
        frame.setLayout(null);
        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testComboBox3() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：组合框-省市两级联动()");

        // 创建container
        Container container = frame.getContentPane();
        // 创建数据容器：省
        DefaultComboBoxModel<Object> provinceComboBoxModel = new DefaultComboBoxModel<>();
        provinceComboBoxModel.addElement("四川省");
        provinceComboBoxModel.addElement("云南省");
        provinceComboBoxModel.addElement("重庆市");
        // 创建数据容器：市
        DefaultComboBoxModel<Object> cityComboBoxModel1 = new DefaultComboBoxModel<>(new String[]{"成都市", "德阳市", "南充市"});
        DefaultComboBoxModel<Object> cityComboBoxModel2 = new DefaultComboBoxModel<>(new String[]{"昆明市", "曲靖市", "玉溪市"});
        DefaultComboBoxModel<Object> cityComboBoxModel3 = new DefaultComboBoxModel<>(new String[]{"涪陵区", "渝中区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "渝北区", "巴南区"});
        // 创建组合框省级和市级的组合框
        JComboBox<Object> provinceComboBox = new JComboBox<>();
        provinceComboBox.setBounds(10, 10, 100, 22);
        provinceComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));

        JComboBox<Object> cityComboBox = new JComboBox<>();
        cityComboBox.setBounds(120, 10, 100, 22);
        cityComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        cityComboBox.setModel(cityComboBoxModel1);

        provinceComboBox.setModel(provinceComboBoxModel);
        provinceComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if ("四川省".equals(e.getItem())) {
                    cityComboBox.setModel(cityComboBoxModel1);
                } else if ("云南省".equals(e.getItem())) {
                    cityComboBox.setModel(cityComboBoxModel2);
                } else if ("重庆市".equals(e.getItem())) {
                    cityComboBox.setModel(cityComboBoxModel3);
                }
            }
        });
        JButton button = new JButton("确定");
        button.setBounds(230, 10, 80, 22);
        button.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        container.add(provinceComboBox);
        container.add(cityComboBox);
        container.add(button);

        // 设置空布局管理器
        frame.setLayout(null);
        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testComboBox3_1() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：组合框-省市两级联动(优化：采用List集合存放DefaultComboBoxModel数据容器，addItemListener()监听器)");

        // 创建container
        Container container = frame.getContentPane();
        // 创建数据容器：省
        DefaultComboBoxModel<Object> provinceComboBoxModel = new DefaultComboBoxModel<>();
        provinceComboBoxModel.addElement("四川省");
        provinceComboBoxModel.addElement("云南省");
        provinceComboBoxModel.addElement("重庆市");
        // 创建数据容器：市
        DefaultComboBoxModel<Object> cityComboBoxModel1 = new DefaultComboBoxModel<>(new String[]{"成都市", "德阳市", "南充市"});
        DefaultComboBoxModel<Object> cityComboBoxModel2 = new DefaultComboBoxModel<>(new String[]{"昆明市", "曲靖市", "玉溪市"});
        DefaultComboBoxModel<Object> cityComboBoxModel3 = new DefaultComboBoxModel<>(new String[]{"涪陵区", "渝中区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "渝北区", "巴南区"});
        // 创建集合
        List<DefaultComboBoxModel<Object>> list = Lists.newArrayList();
        list.add(cityComboBoxModel1);
        list.add(cityComboBoxModel2);
        list.add(cityComboBoxModel3);
        // 创建组合框省级和市级的组合框
        JComboBox<Object> provinceComboBox = new JComboBox<>();
        provinceComboBox.setBounds(10, 10, 100, 22);
        provinceComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));

        JComboBox<Object> cityComboBox = new JComboBox<>();
        cityComboBox.setBounds(120, 10, 100, 22);
        cityComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        cityComboBox.setModel(cityComboBoxModel1);

        provinceComboBox.setModel(provinceComboBoxModel);
        provinceComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    // 判断被选择
                    int index = provinceComboBox.getSelectedIndex();
                    cityComboBox.setModel(list.get(index));
                }
            }
        });
        JButton button = new JButton("确定");
        button.setBounds(230, 10, 80, 22);
        button.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        container.add(provinceComboBox);
        container.add(cityComboBox);
        container.add(button);

        // 设置空布局管理器
        frame.setLayout(null);
        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testComboBox3_2() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：组合框-省市两级联动(优化：采用List集合存放DefaultComboBoxModel数据容器，addActionListener()监听器)");

        // 创建container
        Container container = frame.getContentPane();
        // 创建数据容器：省
        DefaultComboBoxModel<Object> provinceComboBoxModel = new DefaultComboBoxModel<>();
        provinceComboBoxModel.addElement("四川省");
        provinceComboBoxModel.addElement("云南省");
        provinceComboBoxModel.addElement("重庆市");
        // 创建数据容器：市
        DefaultComboBoxModel<Object> cityComboBoxModel1 = new DefaultComboBoxModel<>(new String[]{"成都市", "德阳市", "南充市"});
        DefaultComboBoxModel<Object> cityComboBoxModel2 = new DefaultComboBoxModel<>(new String[]{"昆明市", "曲靖市", "玉溪市"});
        DefaultComboBoxModel<Object> cityComboBoxModel3 = new DefaultComboBoxModel<>(new String[]{"涪陵区", "渝中区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "渝北区", "巴南区"});
        // 创建集合
        List<DefaultComboBoxModel<Object>> list = Lists.newArrayList();
        list.add(cityComboBoxModel1);
        list.add(cityComboBoxModel2);
        list.add(cityComboBoxModel3);
        // 创建组合框省级和市级的组合框
        JComboBox<Object> provinceComboBox = new JComboBox<>();
        provinceComboBox.setBounds(10, 10, 100, 22);
        provinceComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));

        JComboBox<Object> cityComboBox = new JComboBox<>();
        cityComboBox.setBounds(120, 10, 100, 22);
        cityComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        cityComboBox.setModel(cityComboBoxModel1);

        provinceComboBox.setModel(provinceComboBoxModel);
        provinceComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = provinceComboBox.getSelectedIndex();
                cityComboBox.setModel(list.get(index));
            }
        });
        JButton button = new JButton("确定");
        button.setBounds(230, 10, 80, 22);
        button.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        container.add(provinceComboBox);
        container.add(cityComboBox);
        container.add(button);

        // 设置空布局管理器
        frame.setLayout(null);
        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testComboBox4() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：组合框-省市区三级联动");

        // 创建container
        Container container = frame.getContentPane();
        // 创建数据容器：省
        DefaultComboBoxModel<Object> provinceComboBoxModel = new DefaultComboBoxModel<>();
        provinceComboBoxModel.addElement("四川省");
        provinceComboBoxModel.addElement("云南省");
        provinceComboBoxModel.addElement("重庆市");
        // 创建数据容器：市
        DefaultComboBoxModel<Object> cityComboBoxModel1 = new DefaultComboBoxModel<>(new String[]{"成都市", "德阳市"});
        DefaultComboBoxModel<Object> cityComboBoxModel2 = new DefaultComboBoxModel<>(new String[]{"昆明市", "曲靖市"});
        DefaultComboBoxModel<Object> cityComboBoxModel3 = new DefaultComboBoxModel<>(new String[]{"市辖区"});
        // 创建数据容器：区
        DefaultComboBoxModel<Object> areaComboBoxModel11 = new DefaultComboBoxModel<>(new String[]{"锦江区", "武侯区", "青羊区", "金牛区", "成华区"});
        DefaultComboBoxModel<Object> areaComboBoxModel12 = new DefaultComboBoxModel<>(new String[]{"旌阳区", "罗江区", "中江县", "广汉市", "什邡市", "绵竹市"});
        DefaultComboBoxModel<Object> areaComboBoxModel21 = new DefaultComboBoxModel<>(new String[]{"五华区", "盘龙区", "官渡区", "石林彝族自治县", "禄劝彝族苗族自治县"});
        DefaultComboBoxModel<Object> areaComboBoxModel22 = new DefaultComboBoxModel<>(new String[]{"麒麟区", "宣威市", "沾益区", "马龙区", "会泽县"});
        DefaultComboBoxModel<Object> areaComboBoxModel31 = new DefaultComboBoxModel<>(new String[]{"涪陵区", "渝中区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "渝北区", "巴南区"});

        // 创建组合框省级和市级的组合框
        JComboBox<Object> provinceComboBox = new JComboBox<>();
        provinceComboBox.setBounds(10, 10, 100, 22);
        provinceComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        provinceComboBox.setModel(provinceComboBoxModel);

        JComboBox<Object> cityComboBox = new JComboBox<>();
        cityComboBox.setBounds(120, 10, 100, 22);
        cityComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        cityComboBox.setModel(cityComboBoxModel1);

        JComboBox<Object> areaComboBox = new JComboBox<>();
        areaComboBox.setBounds(230, 10, 100, 22);
        areaComboBox.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        areaComboBox.setModel(areaComboBoxModel11);

        provinceComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedProvince = (String) provinceComboBox.getSelectedItem();
                int provinceIndex = provinceComboBox.getSelectedIndex();
                if (provinceIndex == 0) {
                    cityComboBox.setModel(cityComboBoxModel1);
                } else if (provinceIndex == 1) {
                    cityComboBox.setModel(cityComboBoxModel2);
                } else if (provinceIndex == 2) {
                    cityComboBox.setModel(cityComboBoxModel3);
                }
                cityComboBox.setSelectedIndex(0);
            }
        });
        cityComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedProvince = (String) provinceComboBox.getSelectedItem();
                int provinceIndex = provinceComboBox.getSelectedIndex();
                String selectedCity = (String) cityComboBox.getSelectedItem();
                int cityIndex = cityComboBox.getSelectedIndex();
                if (provinceIndex == 0) {
                    cityComboBox.setModel(cityComboBoxModel1);
                    if (cityIndex == 0) {
                        areaComboBox.setModel(areaComboBoxModel11);
                    } else if (cityIndex == 1) {
                        areaComboBox.setModel(areaComboBoxModel12);
                    }
                } else if (provinceIndex == 1) {
                    cityComboBox.setModel(cityComboBoxModel2);
                    if (cityIndex == 0) {
                        areaComboBox.setModel(areaComboBoxModel21);
                    } else if (cityIndex == 1) {
                        areaComboBox.setModel(areaComboBoxModel22);
                    }
                } else if (provinceIndex == 2) {
                    cityComboBox.setModel(cityComboBoxModel3);
                    if (cityIndex == 0) {
                        areaComboBox.setModel(areaComboBoxModel31);
                    }
                }
                areaComboBox.setSelectedIndex(0);
            }
        });
        JButton button = new JButton("确定");
        button.setBounds(340, 10, 80, 22);
        button.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        container.add(provinceComboBox);
        container.add(cityComboBox);
        container.add(areaComboBox);
        container.add(button);

        // 设置空布局管理器
        frame.setLayout(null);
        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
